package com.vincce.sport.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.alibaba.fastjson.JSON;
import com.vincce.sport.core.utils.CheckUtil;
import com.vincce.sport.core.utils.SimpleStorageClient;
import com.vincce.sport.entity.Page;
import com.vincce.sport.entity.PreliminaryContest;
import com.vincce.sport.entity.ShootSpot;
import com.vincce.sport.entity.Team;
import com.vincce.sport.mapper.PreliminaryContestMapper;
import com.vincce.sport.mapper.ShootSpotMapper;
import com.vincce.sport.mapper.TeamMapper;

@Controller
@RequestMapping("/shootSpot")
public class ShootSpotController extends BaseController{

	@Resource
	private ShootSpotMapper shootSpotMapper;
	@Resource
	private TeamMapper teamMapper;
	@Resource
	private PreliminaryContestMapper preliminaryContestMapper;
	
	@RequestMapping("/list")
	public String list(HttpServletRequest request,Model model,
				Page<ShootSpot> page,PreliminaryContest preliminaryContest){
		Map<String, Object> filter = new HashMap<String, Object>();
		Long count = shootSpotMapper.checkQuery(preliminaryContest.getId());
		PreliminaryContest p = preliminaryContestMapper.findPreliminaryContestById(preliminaryContest.getId());
		if(count > 0){
			if(preliminaryContest.getId() != null && !preliminaryContest.getId().equals("")){
				filter.put("preliminaryContestId", preliminaryContest.getId());
			}
			page.setFilter(filter);
			page.setTotal(shootSpotMapper.queryShootSpotCount(page.getFilter()));
			List<ShootSpot> list=shootSpotMapper.queryShootSpotByPage(page.getFilter());
			for (ShootSpot shootSpot : list) {
				Team team = teamMapper.queryTeamById(shootSpot.gettTeamId());
				shootSpot.setTeamName(team.getName());
			}
			page.setResult(replace(list));
			model.addAttribute("page",page);
		}else{
			ShootSpot shootSpot1 = new ShootSpot();
			shootSpot1.settPreliminaryContestId(preliminaryContest.getId().intValue());
			shootSpot1.settTeamId(p.getTeamId().intValue());
			shootSpotMapper.insert(shootSpot1);
			shootSpot1.settTeamId(p.getTeamIdTwo().intValue());
			shootSpotMapper.insert(shootSpot1);
			
			if(preliminaryContest.getId() != null && !preliminaryContest.getId().equals("")){
				filter.put("preliminaryContestId", preliminaryContest.getId());
			}
			page.setFilter(filter);
			page.setTotal(shootSpotMapper.queryShootSpotCount(page.getFilter()));
			List<ShootSpot> list=shootSpotMapper.queryShootSpotByPage(page.getFilter());
			for (ShootSpot shootSpot : list) {
				Team team = teamMapper.queryTeamById(shootSpot.gettTeamId());
				shootSpot.setTeamName(team.getName());
			}
			page.setResult(replace(list));
			model.addAttribute("page",page);
		}
		model.addAttribute("preliminaryContest", p);
		return "/backstage/shootSpot";
	}

	//转换图片路径
	private List<ShootSpot> replace(List<ShootSpot> imgTempList) {
		if(imgTempList!=null && imgTempList.size()>0){
			for(ShootSpot itl : imgTempList){
				itl.setFullTimeUrl(CheckUtil.isEmpty(itl.getFullTimeUrl()) ? "" :String.format(SimpleStorageClient.REQUEST_URI2_PREFIX+itl.getFullTimeUrl(), "ori"));
				itl.setFirstTimeUrl(CheckUtil.isEmpty(itl.getFirstTimeUrl()) ? "" :String.format(SimpleStorageClient.REQUEST_URI2_PREFIX+itl.getFirstTimeUrl(), "ori"));
				itl.setSecondTimeUrl(CheckUtil.isEmpty(itl.getSecondTimeUrl()) ? "" : String.format(SimpleStorageClient.REQUEST_URI2_PREFIX+itl.getSecondTimeUrl(), "ori"));
				itl.setThirdTimeUrl(CheckUtil.isEmpty(itl.getThirdTimeUrl()) ? "" : String.format(SimpleStorageClient.REQUEST_URI2_PREFIX+itl.getThirdTimeUrl(), "ori"));
				itl.setFourthTimeUrl(CheckUtil.isEmpty(itl.getFourthTimeUrl()) ? "" : String.format(SimpleStorageClient.REQUEST_URI2_PREFIX+itl.getFourthTimeUrl(), "ori"));
				itl.setExtraTimrUrl(CheckUtil.isEmpty(itl.getExtraTimrUrl()) ? "" : String.format(SimpleStorageClient.REQUEST_URI2_PREFIX+itl.getExtraTimrUrl(), "ori"));
				itl.setExtraTimrUrl2(CheckUtil.isEmpty(itl.getExtraTimrUrl2()) ? "" : String.format(SimpleStorageClient.REQUEST_URI2_PREFIX+itl.getExtraTimrUrl2(), "ori"));
				itl.setExtraTimrUrl3(CheckUtil.isEmpty(itl.getExtraTimrUrl3()) ? "" : String.format(SimpleStorageClient.REQUEST_URI2_PREFIX+itl.getExtraTimrUrl3(), "ori"));
				itl.setExtraTimrUrl4(CheckUtil.isEmpty(itl.getExtraTimrUrl4()) ? "" : String.format(SimpleStorageClient.REQUEST_URI2_PREFIX+itl.getExtraTimrUrl4(), "ori"));
			}
		}
		return imgTempList;
	}
	
	@RequestMapping("/addOrUpdate")
	@ResponseBody
	public String addOrUpdate(HttpServletRequest request,ShootSpot shootSpot,String url_type,
			@RequestParam(value = "file", required = false) MultipartFile file) throws Exception{
		Long count=0L;
		
        Map<String, Object> result = new HashMap<String, Object>();
        SimpleStorageClient.StorageResult obj = new SimpleStorageClient.StorageResult(200, "");
        if(CheckUtil.isEmpty(shootSpot.getId())){
        	if (file != null && !file.isEmpty()) {
        		String fileName = file.getOriginalFilename();
                String fileType = fileName.substring(fileName.lastIndexOf("."), fileName.length());
                if (!fileType.equals(".jpg") && !fileType.equals(".JPG") && !fileType.equals(".png") && !fileType.equals(".PNG")) {
            		result.put("msg", "格式不正确,只支持后缀名为.jpg或者.png的图片上传。");
            		result.put("suc", false);
                } else {
                	obj = SimpleStorageClient.upload(2, file.getInputStream(), fileType);
                	if (obj.statusCode == 200) {
                		String path = obj.info.replaceAll("\\\\", "/");
                		int temp_type = Integer.parseInt(url_type);
                		switch (temp_type) {
						case 0:
							//全场
							shootSpot.setFullTimeUrl(path);
							break;
						case 1:
							//第一节
							shootSpot.setFirstTimeUrl(path);
							break;
						case 2:
							//第二节
							shootSpot.setSecondTimeUrl(path);
							break;
						case 3:
							//第三节
							shootSpot.setThirdTimeUrl(path);
							break;
						case 4:
							//第四节
							shootSpot.setFourthTimeUrl(path);
							break;
						case 5:
							//加时
							shootSpot.setExtraTimrUrl(path);
							break;
						case 6:
							//加时
							shootSpot.setExtraTimrUrl2(path);
							break;
						case 7:
							//加时
							shootSpot.setExtraTimrUrl3(path);
							break;
						case 8:
							//加时
							shootSpot.setExtraTimrUrl4(path);
							break;
						}
                		shootSpot.setCreateUser(getCurrentUser().getId().intValue());
                		shootSpot.setCreateTime(new Date());
                		count = shootSpotMapper.insert(shootSpot);
                	}
                }
        	}else{
            	result.put("msg","请选择文件!");
                result.put("suc", false);
            }
        }else{
        	if (file != null && !file.isEmpty()) {
        		String fileName = file.getOriginalFilename();
                String fileType = fileName.substring(fileName.lastIndexOf("."), fileName.length());
                if (!fileType.equals(".jpg") && !fileType.equals(".JPG") && !fileType.equals(".png") && !fileType.equals(".PNG")) {
            		result.put("msg", "格式不正确,只支持后缀名为.jpg或者.png的图片上传。");
            		result.put("suc", false);
                } else {
                	obj = SimpleStorageClient.upload(2, file.getInputStream(), fileType);
                	if (obj.statusCode == 200) {
                		String path = obj.info.replaceAll("\\\\", "/");
                		int temp_type = Integer.parseInt(url_type);
                		switch (temp_type) {
						case 0:
							//全场
							shootSpot.setFullTimeUrl(path);
							break;
						case 1:
							//第一节
							shootSpot.setFirstTimeUrl(path);
							break;
						case 2:
							//第二节
							shootSpot.setSecondTimeUrl(path);
							break;
						case 3:
							//第三节
							shootSpot.setThirdTimeUrl(path);
							break;
						case 4:
							//第四节
							shootSpot.setFourthTimeUrl(path);
							break;
						case 5:
							//加时
							shootSpot.setExtraTimrUrl(path);
							break;
						case 6:
							//加时
							shootSpot.setExtraTimrUrl2(path);
							break;
						case 7:
							//加时
							shootSpot.setExtraTimrUrl3(path);
							break;
						case 8:
							//加时
							shootSpot.setExtraTimrUrl4(path);
							break;
						}
                		shootSpot.setUpdateUser(getCurrentUser().getId().intValue());
                		shootSpot.setUpdateTime(new Date());
                		count = shootSpotMapper.update(shootSpot);
                	}
                }
        	}else{
        		shootSpot.setUpdateUser(getCurrentUser().getId().intValue());
        		shootSpot.setUpdateTime(new Date());
        		count = shootSpotMapper.update(shootSpot);
            }
        }
       // result.put("suc", count > 0 ? true : false);
		return JSON.toJSONString(count > 0 ? true : false);
	}
	
	@RequestMapping("/deleteById")
	@ResponseBody
	public String deleteById(HttpServletRequest request,Model model,String id,String url_type){
		long count = 0l;
		Map<String,Object> result = new HashMap<String, Object>();
		ShootSpot shootSpot = new ShootSpot();
		shootSpot.setId(Integer.parseInt(id));
		int temp_type = Integer.parseInt(url_type);
		switch (temp_type) {
		case 0:
			//全场
			shootSpot.setFullTimeUrl("");
			break;
		case 1:
			//第一节
			shootSpot.setFirstTimeUrl("");
			break;
		case 2:
			//第二节
			shootSpot.setSecondTimeUrl("");
			break;
		case 3:
			//第三节
			shootSpot.setThirdTimeUrl("");
			break;
		case 4:
			//第四节
			shootSpot.setFourthTimeUrl("");
			break;
		case 5:
			//加时
			shootSpot.setExtraTimrUrl("");
			break;
		case 6:
			//加时
			shootSpot.setExtraTimrUrl2("");
			break;
		case 7:
			//加时
			shootSpot.setExtraTimrUrl3("");
			break;
		case 8:
			//加时
			shootSpot.setExtraTimrUrl4("");
			break;
		}
		count = shootSpotMapper.update(shootSpot);
		result.put("suc", count > 0 ? true : false);
		return JSON.toJSONString(result);
	}
}
